package com.rahms.server.controller;

import com.rahms.common.constant.MessageConstant;
import com.rahms.common.result.R;
import com.rahms.pojo.entity.MedicationReminderLog;
import com.rahms.server.service.MedicationReminderLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用药提醒日志控制器
 */
@RestController
@RequestMapping("/reminder-log")
public class MedicationReminderLogController {
    
    @Autowired
    private MedicationReminderLogService medicationReminderLogService;
    
    /**
     * 根据用药计划获取提醒日志
     */
    @GetMapping("/by-plan")
    public R<List<MedicationReminderLog>> getLogsByPlanId(@RequestParam Long planId) {
        List<MedicationReminderLog> list = medicationReminderLogService.getLogsByPlanId(planId);
        return R.success(list);
    }
    
    /**
     * 根据老人获取提醒日志
     */
    @GetMapping("/by-elderly")
    public R<List<MedicationReminderLog>> getLogsByElderlyId(@RequestParam Long elderlyId) {
        List<MedicationReminderLog> list = medicationReminderLogService.getLogsByElderlyId(elderlyId);
        return R.success(list);
    }
    
    /**
     * 根据日期范围获取提醒日志
     */
    @GetMapping("/by-date")
    public R<List<MedicationReminderLog>> getLogsByDateRange(@RequestParam Long elderlyId,
                                                           @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
                                                           @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        List<MedicationReminderLog> list = medicationReminderLogService.getLogsByDateRange(elderlyId, startDate, endDate);
        return R.success(list);
    }
    
    /**
     * 确认用药
     */
    @PostMapping("/confirm")
    public R<Boolean> confirmMedication(@RequestParam Long logId, @RequestParam Long userId) {
        boolean result = medicationReminderLogService.confirmMedication(logId, userId);
        return result ? R.success(true) : R.error("确认失败");
    }
    
    /**
     * 添加提醒日志
     */
    @PostMapping("/add")
    public R<Boolean> addReminderLog(@RequestBody MedicationReminderLog log) {
        boolean result = medicationReminderLogService.save(log);
        return result ? R.success(true) : R.error("添加失败");
    }
}